var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function (req, res, next) {
	res.render('system/menu', {
		menus : req.menus,
		title : '菜单配置',
		accountType : BASE_CONFIG.accountType
	});
});

router.getMenu = function (req, res, next) {
	if (req.cert.type == 'anonymous' || !req.cert.account) {
		req.menus = [];
		next();
		return;
	}

	var roles = req.cert.account.roles;

	Db.SystemMenu.find({
		roles : {
			$in : roles
		}
	}).sort({
		sort : 1
	}).exec(function(err, menus) {
		if (err) {
			res.send(500, err);
			return;
		}
		req.menus = menus;
		next();
	});
};

module.exports = router;
